package com.wolves.career.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wolves.career.entity.ProCategory;
import com.wolves.career.entity.ProMajor;
import com.wolves.career.entity.ProProfessional;
import com.wolves.career.entity.vo.MajorQuery;
import com.wolves.career.mapper.ProMajorMapper;
import com.wolves.career.service.ProCategoryService;
import com.wolves.career.service.ProMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wolves.career.service.ProProfessionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 专业 服务实现类
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-31
 */
@Service
public class ProMajorServiceImpl extends ServiceImpl<ProMajorMapper, ProMajor> implements ProMajorService {


    @Autowired
    ProCategoryService categoryService;

    @Autowired
    ProProfessionalService professionalService;

    @Override
    public List<ProMajor> getMajorByProfessionId(String professionId) {
        QueryWrapper<ProMajor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("professional_id", professionId);
        List<ProMajor> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据们类id查询专业
     *
     * @param categoryId
     * @return
     */
    public List<ProMajor> getMajorByCategoryId(String categoryId) {
        QueryWrapper<ProMajor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        List<ProMajor> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据学校id查询专业
     *
     * @param schoolId
     * @return
     */
    private List<ProMajor> getMajorBySchoolId(String schoolId) {
        //得到门类
        List<ProCategory> categoryList = categoryService.getCategoryBySchoolId(schoolId);

        ArrayList<ProMajor> finalList = new ArrayList<>();

        for (ProCategory proCategory : categoryList) {
            String categoryId = proCategory.getId();
            List<ProMajor> majorList = getMajorByCategoryId(categoryId);
            finalList.addAll(finalList);
        }
        return finalList;
    }

    @Override
    public List<ProMajor> getMajorByVo(MajorQuery majorQuery) {
        List<ProMajor> list = new ArrayList<>();
        if (majorQuery.getId() != null) {
            ProMajor proMajor = baseMapper.selectById(majorQuery.getId());
            list.add(proMajor);

        } else if (majorQuery.getProfessionId() != null) {
            list = getMajorByProfessionId(majorQuery.getProfessionId());
        } else if (majorQuery.getSchoolId() != null) {
            list = getMajorBySchoolId(majorQuery.getSchoolId());
        } else {
            list = baseMapper.selectList(null);
        }
        return list;
    }

}
